class CertDataHandler {
  constructor(type, dataInfo, myData){    
    let certGen = new CertGen(type);
    let certType = new CertType(type,dataInfo.filters);
    let certTypeMap = certType.certTypeMap;
    
    let certData = {};
    let genCertDetail = (certItem, item, rank, certSetting,pool) => {
      item["KeyName"]=jsonToString(pool.key);
    }
    let genFullCertData = () => {
      let genCerts = (resultList, certItem, poolKey, idsMap) => {
        let items = certGen.genCert(myData, {genCertDetail, poolKey}, certItem, idsMap);
        resultList.push({certItem, items});
        idsMap[certItem.code] = [];
        (items || []).forEach(pool => {
          if (pool.error) {
            return;
          }
  
          (pool.certList || []).forEach(certDetail => {
            if (certDetail["ids"]){
              (certDetail["ids"]||[]).forEach(id=>{
                idsMap[certItem.code].push(id);
              })
            }else{
              idsMap[certItem.code].push(certDetail["id"]);
            }
  
          })
        })
      };
  
      let idsMapAll = {};
      let allList = [];
      certType.allCertType.forEach(certItem => genCerts(allList, certItem,
        {"certType": certItem.code, "area": "all"}, idsMapAll));
      certData["全国"] = allList;
      let tempMapStr = JSON.stringify(idsMapAll);
      dataInfo.areas.forEach(area => {
        let idsMapArea = JSON.parse(tempMapStr);
        let areaList = [];
        certType.areaCerTypeList.forEach(certItem => genCerts(areaList, certItem, {
          "certType": certItem.code,
          area
        }, idsMapArea));
        certData[area] = areaList;
      })
      return certData;
    }
    genFullCertData();
  
    function getCertList(area = "全国") {
      let list = [];
      let allCertData=certData[area];
      (allCertData||[]).forEach(pool=>{
        let newItem = {certType:pool.certItem,Certs:[] };
        list.push(newItem);
        newItem["certTypeId"] = pool.certItem["code"];
        newItem["CnName"] = pool.certItem["cnName"];
        newItem["EnName"] = pool.certItem["enName"];
        newItem["CnTestName"] = pool.certItem["cnTestName"];
        newItem["EnTestName"] = pool.certItem["enTestName"];
        newItem["Track"]=pool.certItem["mode"];
        newItem["Award"]=pool.certItem["name"];
        (pool.items||[]).forEach(item=>{
          (item.certList||[]).forEach(certItem=>{
            let subItem = JSON.parse(JSON.stringify(certItem.data));
            subItem["_rank"]=certItem["Rank"];
            subItem["Ranking2"]=certItem["Ranking"];
            subItem["TeamName"]=certItem.data["SchoolName"];
            newItem.Certs.push(subItem);
          })
        })
      })
      return list;
    }
  
  
    function getPPTCertList(){
      let certListMap={};
      for(let areaName in certData){
        (certData[areaName]||[]).forEach(pool=>{
          let code = pool.certItem.code;
          let newItem = certListMap[code];
          if(!newItem){
            newItem = {certType:pool.certItem,Certs:[] };
            certListMap[code]=newItem;
            newItem["certTypeId"] = pool.certItem["code"];
            newItem["CnName"] = pool.certItem["cnName"];
            newItem["EnName"] = pool.certItem["enName"];
            newItem["CnTestName"] = pool.certItem["cnTestName"];
            newItem["EnTestName"] = pool.certItem["enTestName"];
            newItem["Track"]=pool.certItem["mode"];
            newItem["Award"]=pool.certItem["name"];
          }
  
          (pool.items||[]).forEach(item=>{
            (item.certList||[]).forEach(certItem=>{
              let subItem = JSON.parse(JSON.stringify(certItem.data));
              subItem["_rank"]=certItem["Rank"];
              subItem["Ranking2"]=certItem["Ranking"];
              subItem["TeamName"]=certItem.data["SchoolName"];
              subItem["certArea"]=areaName;
              if(dataInfo.rankGroupScoreTypes.includes(pool.certItem["scoreType"])){
                subItem["_rankGroup"]=dataInfo.rankGroupMap[subItem["_rank"]];
              }
              newItem.Certs.push(subItem);
            })
          })
        })
      }
      let list = [];
      for(let code in certListMap){
        list.push(certListMap[code]);
      }
      return list;
  
    }
  
    function getStudentAwardsMap() {
  
      let awardMapAll = {};
      let awardMapRegion = {};
  
      $.each(certData, (k, v) => {
        let map = k == "全国" ? awardMapAll : awardMapRegion;
        let setMap = (item,code,preItem)=>{
          if (!map[item["id"]]){
            map[item["id"]]  ={};
          }
          map[item["id"]][code] = preItem["RankingFull"];
        };
        $.each(v, (k2, v2) => {
          let code = v2["certItem"]["code"];
          let isPerson = v2["certItem"]["pgType"] == "person";
          $.each(v2["items"], (kk, vv) => {
            (vv["certList"]||[]).forEach(v3=>{
              if (isPerson){
                setMap(v3, code,v3);
              }else{
                (v3["items"] ||[]).forEach(item =>{
                  setMap(item, code, v3);
                })
              }
            })
  
          })
        })
      });
      return {"all": awardMapAll, "region": awardMapRegion};
    }
  
    function getCertListByName(name, scoreName, maxRank = 10000) {
  
      let teamList = myData[name];
      let list = teamList.sortByRank([scoreName + " desc"], scoreName, maxRank, 0, (item, rank) => {
  
        return {
          "SchoolNumber": item["SchoolNumber"],
          "SchoolRegion": item["SchoolRegion"],
          "School_Cn": item["School_Cn"],
          "School_En": item["School_En"],
          "Name": item["Name"],
          "TeamNumber": item["TeamNumber"],
          "Division": item["Division"],
          "HVS": item["HVS"],
          "Score": toRoundFixed(item[scoreName], 2),
          "TestName": "TeamScore",
          "Rank": rank
        };
      });
      return list;
    }

    function getAwardList() {
      let excelData = certData;
      let awardData = {};
      $.each(excelData, (k, v) => {
        let list = [];
        (v||[]).forEach(g=>{
          (g["list"]||[]).forEach(item =>{
            let newItem = {};
            awardColumns.forEach(cName => newItem[cName] = item[cName]);
            newItem["Award"] = g["award"];
            list.push(newItem);
          });
        });
  
        awardData[k] = list;
      });
      return awardData
    }


    this.getType = ()=>type;
    this.getData =  () => myData;
    this.getCertData = () => certData;
    this.getCertTypeMap = () => certTypeMap;
    this.getCertList = getCertList;
    this.getCertListByName = getCertListByName;
    this.getStudentAwardsMap = getStudentAwardsMap;
    this.getPPTCertList = getPPTCertList;    
  }
  
}
